/*
* Copyright (C) 2019 ~ 2021 Uniontech Software Technology Co.,Ltd.
*
* Author:     yanghongwei <yanghongwei@uniontech.com>
*
* Maintainer: yanghongwei <yanghongwei@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FANOTIFY_H
#define FANOTIFY_H

#include <string>

const int OPEN = 1 ;
const int MODIFY = 2 ;
const int CLOSE_MODIFY = 3;
const int CLOSE = 4 ;

class Fanotify
{
public:
    Fanotify();
    ~Fanotify();

    // 选择检测对象
    void setNotifyObject(std::string path) ;
    // 将fanotify句柄加到select中开始监听
    void startListen() ;

private:
    // 获取句柄
    int getNotifyFD() ;
    //当一些程序试图打开文件的时候，fanotify检测到病给内核发送允许访问标志，当然也可以发送不允许访问标志
    int handlePerm(const struct fanotify_event_metadata* metadata) ;
    //select监测事件
    int selectEvent(fd_set* rfd) ;
    //获取文件操作事件类型
    int getEvent(const struct fanotify_event_metadata* metadata, int len) ;
    //设置打开和关闭事件
    void detectOpenClose() ;
    // 获取文件名跟修改它的进程名称
    std::string getFileName(int fd);
    std::string getProcessName(int pid);

private:
    std::string paths ;
    int fanFd ;
};

#endif // FANOTIFY_H
